package com.example.myapplication.lawyer;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.IdoflawyersBean;
import com.example.myapplication.ShuJu.LawyerlistBean;
import com.example.myapplication.ShuJu.LegalexpertiseBean;
import com.example.myapplication.apter.IdoflawyersApter;
import com.example.myapplication.apter.LawyerlistApter;
import com.example.myapplication.apter.LegalexpertiseApter;
import com.google.gson.Gson;

import java.util.List;

public class AddconsultationlistActivity extends AppCompatActivity {
    private ImageView but_lawyerList;
    private TextView sort_lawyerList;
    private TextView screen_lawyerList;
    private RecyclerView re__legalexpertise;
    private RecyclerView re__legal;
    private List<LegalexpertiseBean.RowsBean> rows;
    private RecyclerView re__lawyerlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addconsultationlist);
        initview();
        //返回
        but_bloak();
        //列表
        lawyerlist();
        //点击排序
        clicksort();
        //点击筛选
        screen();


    }
    private void lawyerlist() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list",
                OKHttpTool.GET,
                LawyerlistBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LawyerlistBean lawyerlistBean = (LawyerlistBean) obj;
                        if (lawyerlistBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    LawyerlistApter lawyerlistBean1 = new LawyerlistApter(AddconsultationlistActivity.this, lawyerlistBean.getRows());
                                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AddconsultationlistActivity.this);
                                    re__lawyerlist.setLayoutManager(linearLayoutManager);
                                    re__lawyerlist.setAdapter(lawyerlistBean1);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }


    private void clicksort() {

    }

    private void screen() {
        screen_lawyerList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(AddconsultationlistActivity.this);
                View inflate = getLayoutInflater().inflate(R.layout.item_sort_lawyerlist, null);
                RecyclerView re_itemsort = inflate.findViewById(R.id.re_itemsort);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(AddconsultationlistActivity.this, 4);
                LegalexpertiseApter legalexpertiseApter = new LegalexpertiseApter(AddconsultationlistActivity.this, rows);
                re_itemsort.setLayoutManager(gridLayoutManager);
                re_itemsort.setAdapter(legalexpertiseApter);
                builder.setView(inflate);

                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //将列表隐藏
                        re__lawyerlist.setVisibility(View.INVISIBLE);
                        Integer ID = legalexpertiseApter.getLawyerID();
                        //根据id获取对应律师列表
                        Log.v("ID","------"+ID);
                        idoflawyers(ID);
                    }
                });
                builder.create().show();
            }
        });
    }

    private void idoflawyers(Integer id) {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list?legalExpertiseId=" + id,
                OKHttpTool.GET,
                IdoflawyersBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        IdoflawyersBean IdoflawyersBean = (IdoflawyersBean) obj;
                        if (IdoflawyersBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    IdoflawyersApter idoflawyersApter = new IdoflawyersApter(AddconsultationlistActivity.this, IdoflawyersBean.getRows());
                                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AddconsultationlistActivity.this);
                                    re__legal.setLayoutManager(linearLayoutManager);
                                    re__legal.setAdapter(idoflawyersApter);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void but_bloak() {
        but_lawyerList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddconsultationlistActivity.this.finish();
            }
        });
    }

    private void initview() {
        Intent intent = getIntent();
        String jsonStringDate = intent.getStringExtra("LawyerListActivity");
        Gson gson = new Gson();
        //rows = gson.fromJson(jsonStringDate, new TypeToken<List<LegalexpertiseBean>>(){}.getType());
        LegalexpertiseBean rowsBean = gson.fromJson(jsonStringDate, LegalexpertiseBean.class);
        rows = rowsBean.getRows();
        but_lawyerList = findViewById(R.id.but_lawyerList);
        sort_lawyerList = findViewById(R.id.sort_lawyerList);
        screen_lawyerList = findViewById(R.id.screen_lawyerList);
        re__legalexpertise = findViewById(R.id.re__Legalexpertise);
        re__lawyerlist = findViewById(R.id.re__lawyerlist);
        re__legal = findViewById(R.id.re__Legal);
    }

}